﻿using System;
using System.Web.Mvc;

using BlueSky.Web.Services;
using BlueSky.Web.IoC;

namespace BlueSky.Web.IoC.Ninject
{
    public class NinjectResolver : IDependencyResolver
    {
        protected IDependencyResolver _controllerResolver = new NinjectControllerResolver();
        protected IDependencyResolver _serviceResolver = new NinjectServiceResolver();

        protected IDependencyResolver ResolverSelector(Type type)
        {
            if (typeof(IController).IsAssignableFrom(type))
                return _controllerResolver;

            if (typeof(IService).IsAssignableFrom(type))
                return _serviceResolver;

            throw new NotSupportedException(type.FullName);
        }



        public object Resolve(Type type)
        {
            return ResolverSelector(type).Resolve(type);
        }

        public T Resolve<T>()
        {
            return (T)Resolve(typeof(T));
        }

        public void Release(object instance)
        {
            ResolverSelector(instance.GetType()).Release(instance);
        }        
    }
}
